//
//  SupportViewController.m
//  SenMo
//
//  Created by ZengYuan on 15/9/25.
//  Copyright © 2015年 ZengYuan. All rights reserved.
//

#import "SupportViewController.h"
#import "SettingCell.h"
#import "RequestViewController.h"
#import "FeedBackViewController.h"
#import "MyLocalAlbumCollectionController.h"
#import "REPhotoThumbnailView.h"
#import "PhotoFetching.h"
#import "LocalPhoto.h"
#import "LocalAlbumCell.h"


@interface SupportViewController ()<UITableViewDataSource,UITableViewDelegate,PhotoFetcherDelegate>
{
    UITableView *tableView;
    NSArray *secArr;
    NSMutableArray *photoArray;
    PhotoFetching* _photoFetcher;
    UIImage *localImage;
}

@end

@implementation SupportViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    CGRect rec = self.view.bounds;
    rec.size.height = kScreenHeight - NAVBARHEIGHT_STATUSBAR - self.tabBarController.tabBar.frame.size.height;
    
    if (!tableView) {
        tableView=[[UITableView alloc]initWithFrame:rec style:UITableViewStylePlain];
        tableView.delegate=self;
        tableView.dataSource=self;
        [self.view addSubview:tableView];
        tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        tableView.backgroundColor=[UIColor clearColor];
        tableView.backgroundView=nil;
    }
    
    self.titleLabel.text = @"发现";
    secArr = @[@"我的消息",@"本地相册",@"寻求帮助"];
    
    
    _photoFetcher = [PhotoFetching sharedPhotoFetcher];
    _photoFetcher.delegate = self;
    [_photoFetcher fetchphotosFromPhotoAlbum];

}

#pragma mark - PhotoFetcherDelegate
//获取相册照片数组
- (void)alubumLoadFinished:(NSMutableArray *)aray{
    if (aray.count > 0) {
        
        photoArray = aray;
        LocalPhoto *photo = photoArray[aray.count - 1];
        
        localImage = photo.thumbnailImage;
        [tableView reloadData];
        
        
    }
    
}
//根据url获取照片   返回image
- (void)photoFetchingDidFinished:(UIImage *)image{
    
    localImage = image;
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [tableView reloadData];
    });
    
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
//    return 1 +  (self.dataArray.count?1:0);
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return secArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 55;
}


- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    if (indexPath.row == 1) {
//        
//        static NSString *ID = @"localAlbum";
//        LocalAlbumCell *cell=[_tableView dequeueReusableCellWithIdentifier:ID];
//        if (!cell) {
//            cell=[[LocalAlbumCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
//            
//        }
//        cell.selectionStyle = UITableViewCellSelectionStyleNone;
//        
//        [cell setCellWithTitle:@"相册" iconURL:nil image:localImage andDetail:@""];
//            
//        cell.rowType = 3;
//
//         return cell;
//
//        
//    }else{
        static NSString *CellId_ES = @"CellIdEs";
        SettingCell *cell=[_tableView dequeueReusableCellWithIdentifier:CellId_ES];
        if (!cell) {
            cell=[[SettingCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellId_ES];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.backgroundColor=[UIColor whiteColor];
            cell.rowType = 3;
        }
        
  
        cell.textLabel.text = secArr[indexPath.row];
        cell.imageView.image = [UIImage imageNamed:cell.textLabel.text];

        return cell;
//    }
    

}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)_tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    if(indexPath.row == 0){
        RequestViewController *rvc = [[RequestViewController alloc]init];
//        rvc.device = self.sdevice;
        rvc.hidesBottomBarWhenPushed=YES;
        [self.navigationController pushViewController:rvc animated:YES];
    }else if(indexPath.row == 1){

        MyLocalAlbumCollectionController *localPhoto = [[MyLocalAlbumCollectionController alloc] init];
        localPhoto.thumbnailViewClass = [REPhotoThumbnailView class];
        localPhoto.localAlbumArray = photoArray;
        localPhoto.hidesBottomBarWhenPushed=YES;
        [self.navigationController pushViewController:localPhoto animated:YES];
        
        
        
    }else if(indexPath.row == 2){
        FeedBackViewController *fbvc = [FeedBackViewController fromSB:@"FeedBackViewController"];
        fbvc.hidesBottomBarWhenPushed=YES;

        [self.navigationController pushViewController:fbvc animated:YES];
    }
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
